﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace AUV5.Web.Service
{
    public partial class UserDetailUpdate : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["userid"] != null)
            {
                string userid = Request["userid"];
                HttpPostedFile photo = Request.Files["photo"];
                string realname = Request["realname"] ?? "";
                string description = Request["description"] ?? "";
                string phoneno = Request["phoneno"] ?? "";
                string qqno = Request["qqno"] ?? "";
                string secretkey=Request["secretkey"]??"";

                if (!IsValidKey(secretkey, userid, photo, realname, description, phoneno, qqno))
                {
                    Response.Write("112");//密钥不正确 
                    return;
                }

                Model.UserInfo user= BLL.UserInfoBLL.Instance.GetDetail(userid);
                if (user != null)
                {
                    if (!string.IsNullOrEmpty(realname))
                    {
                        user.RealName = realname;
                    }
                    if (!string.IsNullOrEmpty(description))
                    {
                        user.Description = description;
                    }
                    if (!string.IsNullOrEmpty(phoneno))
                    {
                        user.PhoneNo = phoneno;
                    }
                    if (!string.IsNullOrEmpty(qqno))
                    {
                        user.QQNo = qqno;
                    }
                    if (photo != null)
                    {
                        Stream stream=null;
                        System.Drawing.Image img =null;
                        try
                        {
                            stream = photo.InputStream;
                            img = System.Drawing.Image.FromStream(stream); 
                            if (img.Width == Common.CommonDefine.avatarMinWidth && img.Height == Common.CommonDefine.avatarMinHeight)
                            {
                                string photopath = base.GetDirectoryOfDate(false);
                                if (!Directory.Exists(Server.MapPath(photopath)))
                                {
                                    Directory.CreateDirectory(Server.MapPath(photopath));
                                }
                                string filename = photopath + Common.FileHelper.GetUniqueFileName() + ".jpg";
                                img.Save(Server.MapPath(filename));
                                user.PhotoPath = filename;
                            }
                            else
                            {
                                Response.Write("314");//"头像尺寸必须为100*100"
                                return;
                            }
                        }
                        finally
                        {
                            //释放资源
                            if (stream != null)
                            {
                                stream.Close();
                                stream.Dispose();
                            }
                            if (img != null)
                            {
                                img.Dispose();
                            }
                        }
                    }

                    user.UpdateDate = DateTime.Now;

                    BLL.UserInfoBLL.Instance.Update(user);
                    Response.Write("100");//修改成功
                }
                else
                {
                    Response.Write("301");//无效的用户
                    return;
                }
            }
            else
            {
                Response.Write("111");//参数不正确 
            }
        }

        private bool IsValidKey(string secretkey, string userid, HttpPostedFile photo, string realname, string description, string phoneno, string qqno)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey = public_key+current_day+userid+realname+description+phoneno+qqno;
                    if (photo != null)
                    {
                        finalkey += photo.ContentLength;
                    }
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }
    }
}